import { Button, Drawer, Popover, List, Radio, Tag } from 'antd';
import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import Move from '../Move'
import { CopyOutlined, SyncOutlined, UserOutlined } from '@ant-design/icons'
// 研发一体化按钮
export function YanFaYiTiHuaBtn() {

    return (
        <div className='jira-btns'>
            <Button className='ivu-btn ' onClick={(e) => { e.stopPropagation(); copyGongsi() }} title="复制公司名称和问题号">公司名</Button>
            <Button className='ivu-btn ' onClick={(e) => { e.stopPropagation(); copyIssue() }} title="复制问题号和问题描述"><CopyOutlined /></Button>
            <Button className='ivu-btn ' onClick={(e) => { e.stopPropagation(); copyPersonInfo() }} title="复制联系人或邮箱"><UserOutlined /></Button>
            <Button className='ivu-btn ' onClick={(e) => { e.stopPropagation(); answerSupportQuestion() }} title="关闭">关闭</Button>
            <span id='deadline' style={{ color: 'red' }}></span>
            {/* <Button onClick={(e) => { toggleJiraDialog() }} title="显示/隐藏 jira修改完成弹窗">显示/隐藏</Button> */}
        </div>
    )
}
export function JiraToolbarBtns() {
    return (
        <Move>
            <div className='jira-right-btns' style={{}}>
                <Button className='ivu-btn ivu-btn-small' onClick={(e) => { answerSupportQuestion('qq', '') }} title="">qq</Button>
                <Button className='ivu-btn ivu-btn-small' onClick={(e) => { answerSupportQuestion('qq', '和后端补丁') }} title="">qq+后端</Button>
                <Button className='ivu-btn ivu-btn-small' onClick={(e) => { answerSupportQuestion('友空间') }} title="">友空间</Button>
                <Button className='ivu-btn ivu-btn-small' onClick={(e) => { answerSupportQuestion('友空间', '和后端补丁') }} title="">友空间+后端</Button>
                <Button className='ivu-btn ivu-btn-small' onClick={(e) => { answerSupportQuestion('挂补丁') }} title="">挂补丁</Button>
            </div>
        </Move>
    )
}
export function YanFaYiTiHuaInit() {
    topButtonInit()
    // if (document.querySelector('.person_name').innerText.includes('游行行')) {
    //     rightButtonInit()
    // }
    if (location.href.includes('https://pm.yyrd.com/login/index')) {
        // 登录页面设置默认焦点
        setTimeout(() => {
            document.querySelector('.verification').click()
            document.querySelector('[name="code"]').focus()
        }, 200);
    }
}
function topButtonInit() {
    // 问题顶部按钮区，不停定位
    let div = document.createElement('div')
    ReactDOM.render(<YanFaYiTiHuaBtn></YanFaYiTiHuaBtn>, div);
    document.body.append(div)
    let fn = () => {
      
        [...document.querySelectorAll('.content_inner .left .field_item')].forEach(item => {
            console.log('item.innerText',item.textContext)
            if (['联系人', 'DSP单据号', '到期时间','DSP产品模块节点','客户名称'].includes(item.querySelector('.label').innerText)) {
                item.querySelector('.value').style.color = 'red'
                item.querySelector('.value').style.fontSize = '16px'
                // item.style.color = 'aqua'
            }
            if (['联系人'].includes(item.querySelector('.label').innerText) && !item.editFlag) {
                if (item.querySelector('.explain_content')) {
                    item.querySelector('.value > div').innerHTML += item.querySelector('.explain_content').innerText
                    item.editFlag = true
                }
            }
            if (['到期时间'].includes(item.querySelector('.label').innerText)) {
                let date = item.querySelector('.value').innerText
                if (date) {
                    document.querySelector('#deadline').innerText = ((new Date(date) - Date.now()) / 1000 / 60 / 60 / 24).toFixed(2) + '天'
                }
            }

        });
    }
    // 循环执行定位
    let timer = setInterval(() => {
        // 列表和详细视图都有的dom元素
        let issueHeader = document.querySelector('.top_left_btn')
        if (issueHeader) {
            let issueHeaderRect = issueHeader.getBoundingClientRect()
            div.style.display = 'block'
            div.style.position = 'fixed'
            // div.style.zIndex = '1111'
            div.style.top = issueHeaderRect.top + 'px'
            div.style.left = issueHeaderRect.left + 300 + 'px'
        } else {
            div.style.display = 'none'
        }
        fn()
    }, 1000);
    // 只执行一次
    setTimeout(() => {
    }, 1000);

}

function rightButtonInit() {
    // 问题顶部按钮区
    let div = document.createElement('div')
    ReactDOM.render(<JiraToolbarBtns></JiraToolbarBtns>, div);
    document.body.append(div)
}


function openQuestionDialog() {
    return new Promise(resolve => {
        let kaishiClick = false;
        [...document.querySelectorAll('.top_btn_box .operations')].forEach(item => {
            if (item.innerText.includes('开始修改')) {
                kaishiClick = true
                item.click()
            }
        })
        setTimeout(() => {
            [...document.querySelectorAll('.top_btn_box .operations')].forEach(item => {
                if (item.innerText.includes('修改完成')) {
                    item.click()
                    resolve()
                }
            })
        }, kaishiClick ? 3200 : 0);
    })
}




// jiraDialog 显示和隐藏
let jiraDialogFlag = true
function toggleJiraDialog() {
    document.querySelector('.jira-dialog').style.display = jiraDialogFlag ? 'none' : 'block'
    jiraDialogFlag = !jiraDialogFlag
}

// 解决问题的描述
export function answerSupportQuestion(type, HouDuan = '') {
    openQuestionDialog().then(() => {
        setTimeout(() => {
            // 在关闭问题的弹窗填值暂不可行
            // let 研发确认问题类型;
            // let 问题解决方式;
            // let 解决方案;
            // [...document.querySelectorAll('.ivu-modal-body .el-form-item')].forEach(item => {
            //     if (item.innerText.includes('研发确认问题类型')) {
            //         研发确认问题类型 = item.querySelector('.ivu-select')
            //     }   
            //     if (item.innerText.includes('问题解决方式')) {
            //         问题解决方式 = item.querySelector('.ivu-select')
            //     }   
            //     if (item.innerText.includes('解决方案')) {
            //         解决方案 = item.querySelector('.ivu-input-type-textarea')
            //     }   
            // })
            // if (type == '挂补丁') {
            //     // 23001dc50196e32149698336f318f7cb3fe2 产品错误
            //     // 6000be638a87a784459395b56f6120702f99 补丁解决 400074559ee17afa412e888ca6808513178a 方案解决
            //     研发确认问题类型.querySelector('.ivu-select-input').value = '产品错误'
            //     研发确认问题类型.parentElement.selected = '23001dc50196e32149698336f318f7cb3fe2'
            //     问题解决方式.querySelector('.ivu-select-input').value = '补丁解决'
            //     问题解决方式.parentElement.selected = '6000be638a87a784459395b56f6120702f99'

            //     let txt = `打问题上挂的补丁,打完补丁请用谷歌浏览器【清除缓存】后复测。【注意】通版补丁会覆盖专项改动和二开改动，若有二开和专项，则不能打此通版补丁`
            //     解决方案.querySelector('textarea').value = txt
            //     解决方案.parentElement.selected = txt
            // } else {
            //     研发确认问题类型.$emit('input',`23001dc50196e32149698336f318f7cb3fe2`)
            //     问题解决方式.$emit('input',`400074559ee17afa412e888ca6808513178a`)
            //     解决方案.$emit('input',`通过${type}发前端合集补丁${HouDuan}给顾问,打完补丁请用谷歌浏览器【清除缓存】后复测。【注意】通版补丁会覆盖专项改动和二开改动，若有二开和专项，则不能打此通版补丁`)
            // }
        }, 2000);
    })

}
// 复制问题号和描述
function copyIssue() {
    let txt = document.querySelector('.title_info_area .code').innerText
        + ' ' + document.querySelector('.title_info_area .title').innerText

    copyStr(txt)
}
// 复制公司名
function copyGongsi() {
    var txt1;
    let banbenhao = '';
    let wentihao = document.querySelector('.title_info_area .code').innerText;
    [...document.querySelectorAll('.content_inner .left .field_item')].forEach(item => {
        if (item.innerText.includes('客户名称')) {
            txt1 = item.querySelector('.view_read_only_label').innerText
        }
        if (item.innerText.includes('模块节点')) {
            banbenhao = item.querySelector('.view_read_only').innerText
        }

    })
    copyStr(`${txt1}(${wentihao},${banbenhao})`)
}
// 复制问题联系人
function copyPersonInfo() {
    var txt1;
    [...document.querySelectorAll('.content_inner .left .field_item')].forEach(item => {
        if (item.innerText.includes('联系人')) {
            txt1 = item.querySelector('.explain_content').innerText.split('|')[1]
        }

    })
    copyStr(txt1)
}
// Jira网站弹窗支持拖拽
function jiraDialogDragPlus() {
    var isMouseDown = false;
    // 累加偏移量
    var boxPosition = { x: 0, y: 0 }
    var startPosition = {}
    document.addEventListener('mousedown', (e) => {
        startPosition.x = e.clientX
        startPosition.y = e.clientY
        if (e.target.classList.contains('ivu-modal-header') || e.target.parentElement.classList.contains('ivu-modal-header')) {
            isMouseDown = true
        }
    })
    document.addEventListener('mouseup', (e) => {
        isMouseDown = false
    })
    document.addEventListener('mousemove', (e) => {
        if (isMouseDown) {
            // 计算位移量
            boxPosition.x += e.clientX - startPosition.x
            boxPosition.y += e.clientY - startPosition.y
            startPosition.x = e.clientX
            startPosition.y = e.clientY
            // document.querySelector('.ivu-modal').style.transform = `translate(${boxPosition.x}px,${boxPosition.y}px)`;
            let allModal = [...document.querySelectorAll('.ivu-modal')];
            allModal.forEach(item => {
                if (item.style.display != 'none') {
                    item.style.transform = `translate(${boxPosition.x}px,${boxPosition.y}px)`;
                }
            })
        }
    })

}
jiraDialogDragPlus()

// 复制方法
function copyStr(content, message) {
    try {

        var url = content; //复制的网址或内容
        var aux = document.createElement('input');
        aux.setAttribute('value', url);
        document.body.appendChild(aux);
        aux.select();
        document.execCommand('Copy'); //原生copy方法执行浏览器复制命令
        document.body.removeChild(aux); //清除input
        if (message == null) {
            /* 复制失败消息 */
        } else {
            /* 复制成功消息 */
        }
    } catch (error) {

    }
}